package cn.tedu.blog.server.controller.api;

import cn.tedu.blog.server.pojo.dto.CommentAddNewDto;
import cn.tedu.blog.server.pojo.vo.CommentListVo;
import cn.tedu.blog.server.security.LoginPrincipal;
import cn.tedu.blog.server.service.ICommentService;
import cn.tedu.blog.server.utils.JsonPage;
import cn.tedu.blog.server.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/comment")
@Slf4j
public class ApiCommentController {
    @Autowired
    private ICommentService commentService;
    
    @GetMapping("/list/{articleId}")
    @ApiOperation("分页查询评论信息")
    public JsonResult listByPage(@PathVariable Long articleId, Integer page, Integer pageSize) {
        JsonPage<CommentListVo> jsonPage = commentService.listByPage(articleId, page, pageSize);
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/insert")
    @ApiOperation("新增评论")
    public JsonResult saveComment(@AuthenticationPrincipal LoginPrincipal loginPrincipal, CommentAddNewDto commentAddNewDto) {
        Long userId = loginPrincipal.getId();
        System.out.println("=========接收到的信息" + commentAddNewDto + " !!!"+ userId + "==========");
        commentAddNewDto.setUserId(userId);
        commentService.saveComment(commentAddNewDto);
        return JsonResult.ok();
    }
//    @GetMapping("/insert")
//    @ApiOperation("新增评论")
//    public JsonResult saveComment(@AuthenticationPrincipal LoginPrincipal loginPrincipal, CommentAddNewDto commentAddNewDto) {
//        Long userId = loginPrincipal.getId();
//        System.out.println("=========接收到的信息" + commentAddNewDto + " !!!"+ userId + "==========");
//        commentAddNewDto.setUserId(userId);
//        commentService.saveComment(commentAddNewDto);
//        return JsonResult.ok();
//    }



    
    @PostMapping("/delete/{id}")
    @ApiOperation("根据id删除评论")
    public JsonResult deleteCommentById(@PathVariable Long id) {
        commentService.deleteCommentById(id);
        return JsonResult.ok();
    }
}
